﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class member_customer : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }        
        if (!UserPower.Contains("Manager"))
        {
            Response.Write("你没有权限！");
            Response.End();
        }
        this.EnableViewState = false;
        Response.Write(Init.InitDefault());
        DataSet ds = DataGroup();        
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            ds.Tables[0].Rows[i]["url"] = herf(ds.Tables[0].Rows[i]["dwbh"].ToString(), "");
        }
         
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "dwbh" };//主键
        GridView1.DataBind();
    }

    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();

    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DataSet ds = DataGroup();
        
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            ds.Tables[0].Rows[i]["url"] = herf(ds.Tables[0].Rows[i]["dwbh"].ToString(), "");
        }
         
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "dwbh" };//主键
        int recordCount = GridView1.Rows.Count;
        //list(spinfo.Text, pihao.Text);
        GridView1.DataBind();
    }

    //展示
    public DataSet DataGroup()
    {       
        string keyword = k.Text;
        string isup = f.Text;
        string khlx = h.Text;

        string sql = "select top 260 dwbh,danwbh,dwmch,lxr,lxrdh,isjh,isxs,'' as url "
        + " from mchk(nolock) where beactive='是'";
        sql += (!string.IsNullOrEmpty(keyword)) ? " and (danwbh like '%" + keyword + "%' or dwmch like '%" + keyword + "%' or zjm like '%" + keyword + "%')" :"";
        sql += (isup == "1") ? " and dwbh not in(select detailid from [ImageDoc](nolock) group by detailid) " : "";
        sql += (isup == "2") ? " and dwbh in(select detailid from [ImageDoc](nolock) group by detailid) " : "";
        sql += (khlx == "1") ? " and isjh='是'" : "";
        sql += (khlx == "2") ? " and isxs='是'" : "";          
        sql+= " order by dwbh desc";
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
        DataSet dss = mssql.ExecuteDs(sql);
        return dss;
    }

    public string herf(string id, string ret)
    {
        DataSet dsmx = new DataSet();
        dsmx.Clear();
        string detailid = id;
        string retsult = "";
        int rowid = 0;
        string sql = "select rtrim(imageid) as imageid,rtrim(url) as url from [ImageDoc](nolock) where detailid=rtrim('" + detailid + "')";
        dsmx = mssql.ExecuteDs(sql);

        retsult = retsult + " <a href=\"/member/customermodify.aspx?id=" + detailid + "\">上传资料</a> ";

        if (dsmx.Tables[0].Rows.Count > 0)
            
			for (int i = 0; i < dsmx.Tables[0].Rows.Count; i++)
            {
                string fileurl = Init.imagefileurl(dsmx.Tables[0].Rows[i]["url"].ToString());
                if (!string.IsNullOrEmpty(fileurl))
                {
                    //retsult += " <a class=\"btn-view preview\" href=\"" + fileurl + "\" rel=\"" + id+ "\" " + (rowid == 0 ? "":" style=\"display:none;\"") + ">预览</a>\r\n";
                    retsult += " <a href=\"" + fileurl + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + fileurl + "\" data-dap=\"" + ret + "\"  class=\"view\" " + (rowid == 0 ? "" : " style=\"display:none;\"") + " style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
					rowid++;
                }
                /*
                if (i == 0)
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                }
                else
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\" style=\"display:none;\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                }
                 */
            }
        return retsult;
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        DataSet ds = DataGroup();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            ds.Tables[0].Rows[i]["url"] = herf(ds.Tables[0].Rows[i]["dwbh"].ToString(), "");
        }
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "dwbh" };//主键
        int recordCount = ds.Tables[0].Rows.Count;
        GridView1.DataBind();
    }
}